﻿using Abp.Domain.Entities;
using GN.Pay.Core.Values;
using GN.Pay.Utils;
using GN.Pay.Utils.Security;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Core.Entities
{
    /// <summary>
    /// 交易通知
    /// </summary>
    [Table("Trade_Notify")]
    [Serializable]
    public class TradeNotify : Entity<long>
    {
        /// <summary>
        /// 应用Id
        /// </summary>
        [Required]
        public string AppId { get; set; }

        /// <summary>
        /// 交易号Id
        /// </summary>
        [Required]
        public string TradeNo { get; set; }

        /// <summary>
        /// 
        /// </summary>
        //通知类型
        public int NotifyType { get; set; }

        //通知Url
        [Required]
        [StringLength(maximumLength: 255)]
        public string NotifyUrl { get; set; }       

        /// <summary>
        /// 返回url
        /// </summary>
        [StringLength(maximumLength: 255)]
        public string ReturnUrl { get; set; }

        private string _UrlParms;

        //通知Url参数
        [Required]
        public string UrlParms
        {
            get
            {
                return _UrlParms;
            }
            set
            {
                _PayNotifyData = null;
                _UrlParms = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private PayNotifyData _PayNotifyData = null;

        /// <summary>
        /// 获取通知数据
        /// </summary>
        /// <returns></returns>
        public PayNotifyData GetPayNotifyData()
        {
            if (_PayNotifyData != null)
            {
                return _PayNotifyData;
            }
            if (string.IsNullOrWhiteSpace(UrlParms))
            {
                return null;
            }
            try
            {
                _PayNotifyData = UrlParms.JsonToObject<PayNotifyData>(true);
                return _PayNotifyData;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 签名
        /// </summary>
        public string Sign { get; set; }

        //通知次数
        public int Count { get; set; }

        /// <summary>
        /// 状态(-1=进入队列失败,1=进入队列成功,2=通知失败,50=通知超过次数, 99=通知成功)
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreationTime { get; set; }

        //最后通知时间
        public DateTime? LastNotifyTime { get; set; }

        //通知消息
        public string NotifyMsg { get; set; }

        private string CreateContent(string key)
        {
            ExceptionUtils.CheckNotNullOrNotWhiteSpace(key, nameof(key));
            SignDictionary signDic = new SignDictionary();
            signDic["appId"] = AppId;
            signDic["tradeNo"] = TradeNo;
            signDic["notifyType"] = NotifyType.ToString();
            signDic["urlParms"] = UrlParms;
            return signDic.UrlLinkString() + "&key=" + key;
        }

        /// <summary>
        /// 数据签名
        /// </summary>
        /// <param name="key">键</param>
        public void DataSign(string key)
        {
            this.Sign = Md5Utils.Sign(CreateContent(key), Encoding.UTF8);
        }

        /// <summary>
        /// 数据签名验证
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public bool DataSginVerify(string key)
        {
            return Md5Utils.SginVerify(CreateContent(key), this.Sign, Encoding.UTF8);
        }


    }
}
